<template>
  <el-card class="box-card" style="margin-bottom: 16px">
    <div slot="header" class="clearfix">
      <!-- 收集数据：v-model="activeName" -->
      <el-tabs v-model="activeName" class="tab">
        <el-tab-pane label="销售额" name="sale" />
        <el-tab-pane label="访问量" name="visit" />
      </el-tabs>
      <div class="right">
        <span @click="setDay">今日</span>
        <span @click="setWeek">本周</span>
        <span @click="setMonth">本月</span>
        <span @click="setYear">本年</span>
        <!-- DatePicker 日期选择器组件。用于选择或输入日期。 -->
        <!--
          value/v-model  属性，绑定值date(DatePicker)/array(DateRangePicker)，用于收集日期选择器组件的数据。
          value-format   属性（可选），绑定值的格式。不指定则绑定值为 Date 对象。
        -->
        <!-- 收集数据：v-model="date" -->
        <el-date-picker v-model="date" class="date" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy=MM-dd"></el-date-picker>
      </div>
    </div>
    <div>
      <!-- Layout 布局组件。通过基础的24分栏，迅速简便地创建布局。18+6 -->
      <el-row :gutter="10">
        <el-col :span="18">
          <!-- 容器 -->
          <div ref="youCharts" class="charts"></div>
        </el-col>
        <el-col :span="6" class="right">
          <h3>门店{{ title }}排名</h3>
          <ul v-if="activeName === 'sale'">
            <li v-for="(rank, index) in listState.orderRank" :key="rank.no">
              <span class="rindex">{{ rank.no }}</span>
              <span>{{ rank.name }}</span>
              <span class="rvalue">{{ rank.money }}</span>
            </li>
          </ul>
          <ul v-else>
            <li v-for="(rank, index) in listState.userRank" :key="rank.no">
              <span class="rindex">{{ rank.no }}</span>
              <span>{{ rank.name }}</span>
              <span class="rvalue">{{ rank.money }}</span>
            </li>
          </ul>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script>
import dayjs from 'dayjs'
import { mapState } from 'vuex'

export default {
  name: 'SaleView',
  data() {
    return {
      activeName: 'sale',
      myCharts: null,
      // 收集日历的数据
      date: []
    }
  },
  computed: {
    title() {
      return this.activeName === 'sale' ? '销售额' : '访问量'
    },
    ...mapState({
      listState: (state) => state.home.list
    })
  },
  watch: {
    title() {
      this.myCharts.setOption({
        title: {
          text: this.title
        },
        xAxis: {
          data:
            this.title === '销售额' ? this.listState.orderFullYearAxis : this.listState.userFullYearAxis
        },
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data: this.title === '销售额' ? this.listState.orderFullYear : this.listState.userFullYear,
            color: 'yellowgreen'
          }
        ]
      })
    },
    listState() {
      this.myCharts.setOption({
        title: {
          text: this.title + '趋势'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: this.listState.orderFullYearAxis,
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data: this.listState.orderFullYear,
            color: 'yellowgreen'
          }
        ]
      })
    }
  },
  mounted() {
    // 初始化ECharts画布实例
    this.myCharts = this.$echarts.init(this.$refs.youCharts)
    // 配置ECharts画布实例
    this.myCharts.setOption({
      title: {
        text: this.title + '趋势'
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: [
        {
          type: 'category',
          data: [],
          axisTick: {
            alignWithLabel: true
          }
        }
      ],
      yAxis: [
        {
          type: 'value'
        }
      ],
      series: [
        {
          name: 'Direct',
          type: 'bar',
          barWidth: '60%',
          data: [],
          color: 'yellowgreen'
        }
      ]
    })
  },
  methods: {
    // 本天
    setDay() {
      const day = dayjs().format('YYYY-MM-DD')
      this.date = [day, day]
    },
    // 本周
    setWeek() {
      const start = dayjs().day(1).format('YYYY-MM-DD')
      const end = dayjs().day(7).format('YYYY-MM-DD')
      this.date = [start, end]
    },
    // 本月
    setMonth() {
      const start = dayjs().startOf('month').format('YYYY-MM-DD')
      const end = dayjs().endOf('month').format('YYYY-MM-DD')
      this.date = [start, end]
    },
    // 本年
    setYear() {
      const start = dayjs().startOf('year').format('YYYY-MM-DD')
      const end = dayjs().endOf('year').format('YYYY-MM-DD')
      this.date = [start, end]
    }
  }
}
</script>

<style scoped>
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}

.tab {
  width: 100%;
}

.right {
  position: absolute;
  right: 0px;
}

.date {
  width: 250px;
  margin: 0px 20px;
}

.right span {
  margin: 0px 10px;
}

.charts {
  width: 100%;
  height: 300px;
}

ul {
  list-style: none;
  width: 100%;
  height: 300px;
  padding: 0px;
}

ul li {
  height: 8%;
  margin: 10px 0px;
}

.rindex {
  float: left;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: indianred;
  color: white;
  text-align: center;
}

.rvalue {
  float: right;
}
</style>
